package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.controller.OrderController;
import com.zretc.dao.OrderMapper;
import com.zretc.entity.Order;
import com.zretc.entity.OrderDetail;
import com.zretc.entity.Product;
import com.zretc.service.ComSaleService;
import com.zretc.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * @author: bLUeSuMMerDreamer
 * @createTime: 2020-08-26 15:39
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired

    private OrderMapper orderMapper;
    @Autowired
    private ComSaleService comSaleService;
    private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Override
    public List<Order> getOrdersByParams(Map<String, Object> params) {
        return orderMapper.getOrdersByParams(params);
    }

    @Override
    public PageInfo<OrderDetail> getOrderDetailsByParams(Map<String, Object> params) {
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<OrderDetail> orderDetailsByParams = orderMapper.getOrderDetailsByParams(params);
        PageInfo<OrderDetail> pageInfo = new PageInfo<>(orderDetailsByParams);
        return pageInfo;
    }

    @Override
    public List<Map<String, Object>> getStatistics(Map<String, Object> params) {
        logger.debug("=============" + params);
        String key;
        if (params.get("groupBy") != null) {
            key = (String) params.get("groupBy");
        } else {
            key = "";
        }
        switch (key) {
            case "productId":
                return orderMapper.getSumGroupByProductId(params);
            case "productName":
                return orderMapper.getSumGroupByProductName(params);
            case "typeName":
                return orderMapper.getSumGroupByTypeName(params);
            case "commentScore":
                return orderMapper.getCountGroupByCommentScore(params);
            case "":
                Map<String, Object> map = new HashMap<>();
                List<Map<String, Object>> list = new Vector<>();
                Integer getCountComment = comSaleService.getCountCommentBySellerAccount((String) params.get("sellerAccount"));
                Integer getCountSales = comSaleService.getCountSalesBySellerAccount((String) params.get("sellerAccount"));
                List<Map<String, Object>> totalTurnoverBySellerAccount = orderMapper.getTotalTurnoverBySellerAccount(params);
                map.put("buyers", getCountSales);
                map.put("totalComment", getCountComment);
                map.putAll(totalTurnoverBySellerAccount.get(0));
                list.add(map);
                return list;
            default:
                return orderMapper.getStatistics(params);
        }

    }
}
